﻿package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * TODO
	 * 注意此类有一个严重的问题和动画相关,使用时要注意不要使用gotoAndPlay这个方法 ,这个方法问题很多
	 * 使用gotoAndStop方法,通过ENTER_FRAME的方式来实现动画
	 * 
	 * 每次操作时,把相应的startFrame endFrame 配置好即可
	 */ 
	public class ChipEffect extends MovieClip
	{
		public var chip:ChipObject;
		
		private var startFrame:int = -1;
		private var endFrame:int = -1;
		
		private var func:Function = null;//函数用来控制循环播放
		
		private var _data:Object;//存储颜色跟数字变量
		
		
		
		public function ChipEffect()
		{
			super();
			this.stop();
			this.configEvent();
		}
		
		/**
		 * 添加鼠标事件
		 * 鼠标移动到筹码时候
		 * 鼠标移出筹码的时候
		 * */
		
		private function configEvent():void {
			this.addEventListener(MouseEvent.ROLL_OVER,rollOverHandler);
			this.addEventListener(MouseEvent.ROLL_OUT,rollOutHandler);
		}
		
		private function checkState():void {
			this.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
			this.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
		}
		
		
		/**
		 * 鼠标移到筹码的时候执行的函数
		 * 判断当前筹码是否为选中的状态如果选中则不执行
		 * */
		private function rollOverHandler(event:MouseEvent):void {
			if(this.selected){
				return;
			}
			this.checkState();
			///鼠标移动到筹码的效果从第0帧开始到第五帧
			startFrame = 1;
			endFrame = 5;
			//this.gotoAndStop(endFrame);//结束了停在第五帧
		}
		 
		
		/**
		 * 鼠标移出筹码的时候执行的函数
		 * 判断当前筹码是否为选中的状态如果选中则不执行
		 * */
		
		private function rollOutHandler(event:MouseEvent):void {
			if(this.selected){
				return;
			}
			this.checkState();
			this.startFrame = 6;
			this.endFrame = 10;
			//this.gotoAndPlay(startFrame);
			
		}
		
		
		
		/**
		 *EnterFrame事件 指定帧数逐帧播放，
		 *  **/
		private function onEnterFrameHandler(event:Event):void {
			
			startFrame++;
			
			if(startFrame > endFrame)//如果开始的帧数大于结尾的帧数
			{
				this.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
				if(this.func != null)
				{
					this.func();
				}
			}
			else 
			{
				this.gotoAndStop(startFrame);
				//传入颜色跟数字参数用来显示筹码
				if(this.chip)
				{
					this.chip.data = this._data;
				}
				else 
				{
					//trace(this.currentFrame);
				}
			}
			
		}
		
		private function func10():void {
			this.stop();
		}
		
		
		
		private function selectedFunc():void {
			this.checkState();
			this.startFrame = 11;
			this.endFrame = 22;
			this.gotoAndStop(this.startFrame);
			this.func = selectedFunc;
		}
		
		
		
		public function set data(value:Object):void {
			this._data = value;
			this.update();
		}
		
		public function get data():Object {
			return this._data;
		}
		
		public function update():void {
			this.chip.data = this._data
		}
		
		private var _selected:Boolean;
		
		
		/**
		 * 判断当前筹码是否为选中的状态
		 * 如果选中的话那么激活跳动状态
		 * */		
		public function set selected(value:Boolean):void {
			if(this._selected != value){
				this._selected = value;
				if(this._selected){
					this.selectedFunc();
				}else {
					this.normal();
				}
			}
		}
		
		/**
		 * 改变选中目标时执行停止跳动状态
		 * */
		public function normal():void {
			this.func = null;
			this.checkState();
			this.startFrame = 8;
			this.endFrame = 9;
		}
		
		public function get selected():Boolean {
			return this._selected;
		}
		
		public function clone():ChipEffect {
			var chipEffect:ChipEffect = new ChipEffect();
			chipEffect.data = this._data;
			return chipEffect;
		}
	}
}